define(function(require, exports){
	var pubjs = require('pubjs');
	var baseGrid = require('@base/grid').baseNoDate;
	var $ = require('jquery');
	var util = require('util');

	var Base = baseGrid.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'sort': null,
				'hasTab': false,
				'hasPager': false,
				'hasAmount': false,
				'hasExport': false,
				'hasSearch': false,
				'emptyText': LANG('请选择收费维度设置规则'),
				'cols': [
					{name: 'Cat', text: LANG('维度'), render: 'renderDim', align:'center', sort: false, width:80},
					{name: 'Ids', text: LANG('规则对象'), render: 'renderIds', width:400, align:'left', sort: false},
					{name: 'Type', text: LANG('收费规则'), render: 'renderRule', align:'left', width:250, sort: false}
				]
			});

			this.$CHANNEL = pubjs.config('exchange');
			this.Super('init', arguments);
			this.$data = [];
		},
		// 渲染维度
		renderDim: function (index, val, data, con) {
			return {
				'Channel': LANG('渠道')
				,'Spot': LANG('广告位')
			}[val];
		},
		// 渲染规则对象
		renderIds: function (i, val, data, con) {
			if(!this.$data[i]){
				this.$data[i] = {};
			}
			var cat = data.Cat;
			var tmp = '';
			switch (cat){
				case 'Channel':
					tmp = this.createChannelText(val);
					break;
				case 'Spot':
					tmp = LANG('共%1个广告位', val.length);
					break;
			}
			this.$data[i].label =  $('<span class="M-tableListWidthLimit"/>').attr('title',tmp).width(380).text(tmp);
			return this.$data[i].label;
		},
		// 渲染收费规则
		renderRule: function (i, val, data, con) {
			var self = this;

			if(!self.$data[i]){
				self.$data[i] = {};
			}

			data.Type = data.Type || 1;

			self.$data[i].type	= data.Cat;
			self.$data[i].ids	= data.Ids;
			self.$data[i].rule	= data.Type;

			// 构建元素
			var content = $([
				'<div class="P-infoMoreRuleTableDP">',
					'<div class="dropdownCon"></div>',
					'<input class="chargeAmount" type="text" data-index="'+i+'" placeholder="0"/>',
					'<span class="chargeUnit">'+LANG('')+'</span>',
				'</div>'
			].join(''));
			self.$data[i].input = content.find('.chargeAmount').val(data.Value||'');
			// content.find('.chargeUnit').text('%');
			content.find('.chargeUnit').text(data.Type <= 1 ? '%' : LANG('元'));

			// 如果已经存在模块，先清除，避免重新渲染的时候重复模块名字而出错
			if(self.get('dropdown'+i)){
				self.get('dropdown'+i).destroy();
			}

			var dpOptions = data.Type == 2 ? [
				{'Name':LANG('溢价'), '_id': 1},
				{'Name':LANG('CPM'), '_id': 2}
			] : self.getConfig('dpOptions');

			// 重新构建dropdown
			self.createAsync('dropdown'+i, '@base/common/drop.drop', {
				'def': LANG('收费规则'),
				'target': content.find('.dropdownCon'),
				'options': dpOptions,
				'name': 'Name',
				'key': '_id',
				'width': 120,
				'value': 1,
				'panel_config': {
					'appendTo': 'popup',
					'hasSearch': false
				}
			}, function(mod){
				self.$data[i].dropdown = mod;
				mod.setValue(data.Type);
				if(data.Type == 2){
					content.find('.chargeAmount').toggleClass('is-disabled', true);
					mod.disable();
				}
			});
			return content;
		},
		onDropChange: function(ev){
			var name = ev.name;
			var id = ev.param.id;
			// 找到单位元素
			var unit_el = this.get(name).getDOM().parent().siblings('.chargeUnit');

			switch (id){
				case '1':
					unit_el.text('%'); // 溢价用百分比
					break;
				default:
					unit_el.text(LANG('元')); // 其他用元
			}

			return false;

		},
		createChannelText: function(data){
			var tmp = [];
			var self = this;
			util.each(data, function(val){

				if(util.isObject(val)){
					tmp.push(val.name);
				}

				if(util.isNumber(val)){
					util.each(self.$CHANNEL, function(item, idx){
						if(val && val == item.id){
							tmp.push(item.name);
						}
					});
				}
			});
			tmp = tmp.join('、');
			return tmp;
		},
		updateRow: function(id, data){
			var self = this
				, da = self.$data[id];
			if(da) {
				da.ids = data.Ids;
				if(data.Cat == 'Channel'){
					da.label.text(self.createChannelText(data.Ids));
				}
				else {
					da.label.text(LANG('共%1个广告位', data.Ids.length));
				}
			}
		},
		removeRow: function(id){
			//this.Super('removeRow', arguments);
			var el = this.getContainer();
			el.find('tr[data-index="'+id+'"]').css('display','none');
			var self = this
				, da = self.$data[id];
			if(da) {
				self.$data[id] = null;
			}
			return false;
		},
		// 下拉框选项选中事件
		onOptionChange: function(ev){
			var name = ev.name;
			var id = ev.param.id;
			// 找到单位元素
			var unit_el = this.get(name).el.parent().parent().find('.chargeUnit');

			switch (id){
				case '1':
					unit_el.text('%'); // 溢价用百分比
					break;
				default:
					unit_el.text(LANG('元')); // 其他用元
			}

			return false;
		},
		addRow: function(){
			this.$.list.addRow.apply(this.$.list, arguments);
		},
		getData: function(){
			var ret = [];
			var obj;
			util.each(this.$data, function(data){
				if(data){
					obj = {
						'Cat': data.type
						,'Ids': data.ids
						,'Value': +data.input.val() || 0
						,'Type': +data.dropdown.getValue(true).id || 0
					};
					ret.push(obj);
				}
			});

			return ret;
		}
	});

	exports.base = Base;

});